package com.jingxuan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jingxuan.common.result.Result;
import com.jingxuan.dto.request.AddCommentRequest;
import com.jingxuan.dto.request.GetCommentsRequest;
import com.jingxuan.dto.response.AddCommentsResponse;
import com.jingxuan.dto.response.GetCommentsResponse;
import com.jingxuan.service.CommentsService;
import com.jingxuan.interceptor.AuthInterceptor;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

/**
 * 评论控制器
 */
@Slf4j
@RestController
public class CommentsController {
    
    @Autowired
    private CommentsService commentsService;
    
    /**
     * 添加评论
     * POST /products/{productId}/comments
     */
    @PostMapping("/products/{productId}/comments")
    public Result<AddCommentsResponse> addComment(@PathVariable String productId,
                                                  @Valid @RequestBody AddCommentRequest request) {
        log.info("添加评论，商品ID：{}，请求参数：{}", productId, request);
        AddCommentsResponse response = commentsService.addComment(productId, request);
        return Result.success(response);
    }

    /**
     * 查看商品评论（分页）
     * GET /products/{productId}/comments?page=1&pageSize=10
     */
    @GetMapping("/products/{productId}/comments")
    public Result<GetCommentsResponse> getComments(@PathVariable String productId,
                                                     @RequestParam(required = false) Integer page,
                                                     @RequestParam(required = false) Integer pageSize) {
        log.info("查看商品评论，商品ID：{}，页码：{}，每页条数：{}", productId, page, pageSize);
        
        GetCommentsRequest request = new GetCommentsRequest();
        request.setProductId(productId);
        request.setPage(page);
        request.setPageSize(pageSize);
        
        GetCommentsResponse response = commentsService.getComments(productId, request);
        return Result.success(response);
    }

    /**
     * 查看用户自己的评论（分页）
     * GET /users/me/comments?page=1&pageSize=10
     */
    @GetMapping("/users/me/comments")
    public Result<GetCommentsResponse> getMyComments(@RequestParam(required = false) Integer page,
                                                     @RequestParam(required = false) Integer pageSize) {
        // 获取当前用户的id
        Long userId = AuthInterceptor.getCurrentUserId();
        log.info("查看自己的评论，用户ID：{}", userId);
        GetCommentsRequest request = new GetCommentsRequest();
        request.setPage(page);
        request.setPageSize(pageSize);
        GetCommentsResponse response = commentsService.getMyComments(userId, request);
        return Result.success(response);
    }
}
    
